package android.FiltrosJava;
import java.awt.image.*;

public abstract class Histogram extends AbstractImageBasic {
	int channel;
	public void setChannel(int myChannel){
		channel = myChannel;		
	}
	public abstract BufferedImage applyFilter(BufferedImage src);
	
	public int[] computeHistogram(BufferedImage src, int channel){
		int[] h = new int[256];
				
		for (int y = 0; y < 256; y++){
			h[y] = 0;
		}
		
		int width = src.getWidth();
        int height = src.getHeight();             
    	
    	WritableRaster raster_src = src.getRaster();    	
    	int[] imageData = new int[raster_src.getNumBands()];
    	
    	for ( int y = 0; y < height; y++ ) {
    		for ( int x = 0; x < width; x++ ) {
    			raster_src.getPixel(x,y,imageData);
    			h[imageData[channel]]++;    			    			
    		}
    	} 
		
		return h;
	}
	
	public int[] acumuleNormalizeHistogram(int[] h){
		int[] acumH = new int[256];
		acumH[0] = h[0];
		for (int y = 1; y < 256; y++){
			acumH[y] = h[y] + acumH[y-1];
		}		
		
		int maior = acumH[255];
		for (int y = 0; y < 256; y++){
			acumH[y] = (short) (255 * acumH[y]/maior);
		}
		
		return acumH;
	}
}
